package case4;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

class MyThreadPoolExecutor{
    private List<Thread> threadList = new ArrayList<>();
    private BlockingDeque<Runnable> queue = new LinkedBlockingDeque<>();

    public MyThreadPoolExecutor(int n) {
        for(int i = 0; i < n; i++){
            Thread thread = new Thread(() -> {
                while(true){
                    try {
                        Runnable runnable = queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            thread.start();
//            threadList.add(thread);
        }
    }

    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
}

public class Demo3 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPoolExecutor executor = new MyThreadPoolExecutor(4);
        for(int i = 0; i < 1000; i++){
            int n = i;
            executor.submit(new Runnable() {
                @Override
                public void run() {
//                    try {
//                        Thread.sleep(1000);
//                        System.out.println("执行任务 " + n + "，当前线程为：" + Thread.currentThread().getName());
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
                    System.out.println("执行任务 " + n + "，当前线程为：" + Thread.currentThread().getName());
                }
            });
        }
    }
}
